//
//  ZYXHighQualityVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/4.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXHighQualityVC.h"
#import "ZYXVideoPlayVC.h"

#import "ZYXHighQualityCell.h"
#import "ZYXClassificationModel.h"
@interface ZYXHighQualityVC ()
@property (strong,nonatomic) NSMutableArray *dataArr;
///页数
@property (strong,nonatomic) NSString *page;
@end

@implementation ZYXHighQualityVC

- (void)viewDidLoad {
    [super viewDidLoad];

}
#pragma mark MainUI
-(void)setupViews{
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXHighQualityCell" bundle:nil] forCellReuseIdentifier:@"ZYXHighQualityCell"];
    [self.view addSubview:self.tableView];
    [self setupRefresh];
    self.page = @"1";
    [self loadRequestPage:@"1"];
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    if ([self.typeStr isEqualToString:@"1"]) {
        [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_IndexNewRelase) parmas:@{@"page":page} successBloock:^(NSDictionary *responseObject) {
            NSLog(@"responseObject=%@",responseObject);
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                
                if (self.page <= responseObject[@"xin"][@"last_page"]) {
                    
                    NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"xin"][@"data"]];
                    [self.dataArr addObjectsFromArray:modelArr];
                    [self.tableView reloadData];
                    
                }else{
                    [JKToast showWithText:@"暂无加载更多内容"];
                }
                
                
            }else{
                [JKToast showWithText:responseObject[@"message"]];
            }
            [self endRefresh];
            
        } failureBlock:^(NSError *error) {
            [self endRefresh];
        }];
    }else{
        [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_IndexNewRecommended) parmas:@{@"page":page} successBloock:^(NSDictionary *responseObject) {
            NSLog(@"responseObject=%@",responseObject);
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                
                if (self.page <= responseObject[@"tuijian"][@"last_page"]) {
                    
                    NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"tuijian"][@"data"]];
                    [self.dataArr addObjectsFromArray:modelArr];
                    [self.tableView reloadData];
                    
                }else{
                    [JKToast showWithText:@"暂无加载更多内容"];
                }
                
                
            }else{
                [JKToast showWithText:responseObject[@"message"]];
            }
            [self endRefresh];
            
        } failureBlock:^(NSError *error) {
            [self endRefresh];
        }];
    }
   
}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXHighQualityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXHighQualityCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  HEIGHT(100);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击");
    ZYXClassificationModel *model = self.dataArr[indexPath.row];
    ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
    vc.title = model.title;
    vc.zid = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
